package com.yc.bdw.Adapter;

import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yc.bdw.Activity.HomeActivity;
import com.yc.bdw.Activity.ShoppingCarActivity;
import com.yc.bdw.R;
import com.yc.bdw.model.ShopCarItem;

import org.kymjs.kjframe.KJActivity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class ShopCarListAdapter extends BaseAdapter {

	private static HashMap<Integer,Boolean> isSelected;
	private List<ShopCarItem> list = new ArrayList<ShopCarItem>();
	private LayoutInflater inflater = null;
	private Context context = null;

	private TextView priceAllText;
	private TextView numberText;
	private CheckBox selectAll;
	private BigDecimal priceAll;
	private int num = 1;

	public ShopCarListAdapter(){}

	public ShopCarListAdapter(Context context) {
		inflater = LayoutInflater.from(context);
		this.context = context;

		KJActivity activity = (KJActivity)context;
		priceAllText = (TextView) activity.findViewById(R.id.shopcar_price);
		numberText = (TextView) activity.findViewById(R.id.shopcar_number);
		selectAll = (CheckBox) activity.findViewById(R.id.shopcar_selectAll);
	}

	public void init(){
		isSelected = new HashMap<Integer, Boolean>();
		for ( int i = 0; i < list.size(); i++ ) {
			isSelected.put(i,false);
		}
		priceAll = new BigDecimal(0);
	}
	
	@Override
	public int getCount() {
		if ( list != null ) {
			return list.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public static class ShopCarItemView {
		TextView name;
		TextView price;
		TextView describe;
		ImageView img;
		CheckBox isselected;
		EditText number;
		ImageView minusBtn;
		ImageView addBtn;
		View info;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ShopCarItemView shopCarItem;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_shoppingcar, null);
			shopCarItem = new ShopCarItemView();
			shopCarItem.name = (TextView) convertView.findViewById(R.id.item_shopcar_name);
			shopCarItem.price = (TextView) convertView.findViewById(R.id.item_shopcar_price);
			shopCarItem.describe = (TextView) convertView.findViewById(R.id.item_shopcar_describe);
			shopCarItem.img = (ImageView) convertView.findViewById(R.id.item_shopcar_imagepath);
			shopCarItem.minusBtn = (ImageView) convertView.findViewById(R.id.item_shopcar_number_minus);
			shopCarItem.addBtn = (ImageView) convertView.findViewById(R.id.item_shopcar_number_add);
			shopCarItem.isselected = (CheckBox) convertView.findViewById(R.id.item_shopcar_checked);
			shopCarItem.number = (EditText) convertView.findViewById(R.id.item_shopcar_number);
			shopCarItem.info = convertView.findViewById(R.id.item_shopcar_info);

			convertView.setTag(shopCarItem);
		} else {
			shopCarItem = (ShopCarItemView) convertView.getTag();
		}

		shopCarItem.name.setText(list.get(position).getProductName());
		shopCarItem.price.setText("￥"+list.get(position).getPrice().multiply(BigDecimal.valueOf(list.get(position).getNumber())));
		shopCarItem.describe.setText(""+list.get(position).getDescribe());
		shopCarItem.number.setText(""+list.get(position).getNumber());

//		String path = list.get(position).getImagePath();
//		shopCarItem.img.getLayoutParams().width = ListUtils.dip2px(context, 74);
//		shopCarItem.img.getLayoutParams().height = ListUtils.dip2px(context, 78);
//		KJBitmapUtils.getObj().display(shopCarItem.img, path);

		shopCarItem.img.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				((KJActivity)context).showActivity(((KJActivity)context), HomeActivity.class);
			}
		});

		shopCarItem.info.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				((KJActivity)context).showActivity(((KJActivity)context), HomeActivity.class);
			}
		});

		shopCarItem.addBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				int number = Integer.parseInt(shopCarItem.number.getText().toString());
				number++;
				shopCarItem.number.setText("" + number);
				shopCarItem.price.setText("" + list.get(position).getPrice().multiply(BigDecimal.valueOf(number)));

				if (isSelected.get(position)) {
					priceAll = priceAll.add(list.get(position).getPrice());
					priceAllText.setText(priceAll.toString());
				}
				list.get(position).setNumber(number);
			}
		});

		shopCarItem.minusBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				int number = Integer.parseInt(shopCarItem.number.getText().toString());
				if (number > 1) {
					number--;
					shopCarItem.number.setText("" + number);
					shopCarItem.price.setText("" + list.get(position).getPrice().multiply(BigDecimal.valueOf(number)));

					if (isSelected.get(position)) {
						priceAll = priceAll.subtract(list.get(position).getPrice());
						priceAllText.setText(priceAll.toString());
					}
					list.get(position).setNumber(number);
				}
			}
		});

		shopCarItem.number.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View view, MotionEvent motionEvent) {
				num = Integer.parseInt(shopCarItem.number.getText().toString());
				shopCarItem.number.setCursorVisible(true);
				shopCarItem.number.setFocusable(true);
				return false;
			}
		});

		shopCarItem.number.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView textView, int action, KeyEvent keyEvent) {
				if (action == EditorInfo.IME_ACTION_DONE) {
					String numStr = shopCarItem.number.getText().toString();
					if ("".equals(numStr) || "0".equals(numStr)) {
						numStr = "1";
					}
					shopCarItem.number.setCursorVisible(false);
					shopCarItem.number.setFocusable(false);
					int number = Integer.parseInt(numStr);
					shopCarItem.price.setText("" + list.get(position).getPrice().multiply(BigDecimal.valueOf(number)));
					if (isSelected.get(position)) {
						priceAll = priceAll.add(list.get(position).getPrice().multiply(
								BigDecimal.valueOf(number - num)));
						priceAllText.setText(priceAll.toString());
					}
					list.get(position).setNumber(number);
				}
				return false;
			}
		});

		shopCarItem.isselected.setChecked(isSelected.get(position));
		shopCarItem.isselected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
				BigDecimal price = new BigDecimal(shopCarItem.price.getText().toString());
				if ( isChecked ) {
					if ( !ShoppingCarActivity.isEdit() ) {
						priceAll = priceAll.add(price);
						priceAllText.setText(priceAll.toString());
						numberText.setText("" + (Integer.valueOf(numberText.getText().toString()) + 1));
						getIsSelected().put(position,true);
					} else {
						getIsSelected().put(position, true);
						if ( !"0".equals(numberText.getText().toString()) ) {
							priceAll = new BigDecimal(0);
							priceAllText.setText(priceAll.toString());
							numberText.setText("0");
						}
					}
				} else {
					if ( !ShoppingCarActivity.isEdit() ) {
						priceAll = priceAll.subtract(price);
						priceAllText.setText(priceAll.toString());
						numberText.setText(""+(Integer.valueOf(numberText.getText().toString())-1));
						getIsSelected().put(position,false);
					} else {
						getIsSelected().put(position, false);
						if ( !"0".equals(numberText.getText().toString()) ) {
							priceAll = new BigDecimal(0);
							priceAllText.setText(priceAll.toString());
							numberText.setText("0");
						}
					}
				}
			}
		});

		return convertView;
	}

	public List<ShopCarItem> getList() {
		return list;
	}

	public void setList(List<ShopCarItem> list) {
		this.list = list;
	}

	public static HashMap<Integer,Boolean> getIsSelected() {
		return isSelected;
	}

	public static void setIsSelected(HashMap<Integer,Boolean> isSelected) {
		ShopCarListAdapter.isSelected = isSelected;
	}
}